import { combineReducers } from 'redux';
import { login } from './loginReducer';
import { cityName } from './homeReducer';
import { counter } from './counterReducer';
import { default as products, getProducts } from './productReducer';
import { default as cart, getAddedIds, getQuantity } from './cartReducer';
export default combineReducers({
  login,
  cityName,
  counter,
  products,
  cart
})

export function getCartProducts(state) {
  return getAddedIds(state.cart).map((id) => ({
    ...getProducts(state.products, id),
    quantity: getQuantity(state.cart, id)
  }))
}

export function getTotal(state) {
  return getAddedIds(state.cart).reduce((total, id) => {
    total += getProducts(state.products, id).price * getQuantity(state.cart, id)
    return total
  }, 0)
}

export function getMessage(state) {
  return state.cart.checkoutStatus.message;
}

export function getCheckoutPending(state) {
  return state.cart.checkoutStatus.checkoutPending;
}

export function getCart(state) {
  return state.cart;
}